import java.util.concurrent.CountDownLatch;

public class Test {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch=new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            int id=i;
            Thread t=new Thread(()->{

                   System.out.println("线程"+id+"正在下载中");
                   try {
                       Thread.sleep(2000);
                   } catch (InterruptedException e) {
                       throw new RuntimeException(e);
                   }
                   System.out.println("线程"+id+"完成下载");
                   latch.countDown();


            });
            t.start();

        }
        latch.await();
        System.out.println("所有文件下载完毕");
    }
}



/*
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Test {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable callable=new Callable() {
            int resault=0;
            @Override
            public Object call() throws Exception {
                for (int i = 1; i <= 1000; i++) {
                    resault+=i;
                }
                return resault;
            }
        };


        FutureTask futureTask=new FutureTask(callable);

        Thread t=new Thread(futureTask);
        t.start();
        System.out.println(futureTask.get());
    }
}
*/


/*
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class MyThreadPoolExecurtor{
    private List<Thread> threadList=new ArrayList<>();
    private BlockingQueue<Runnable> queue=new ArrayBlockingQueue<>(1000);
    public MyThreadPoolExecurtor(int n){
        for (int i = 0; i < n; i++) {
            Thread t=new Thread(()->{
                try {
                    Runnable runnabler=queue.take();
                    runnabler.run();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            threadList.add(t);
            
        }
    }

    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}
public class Test {
}
*/



/*
class Singleton{
    private static Singleton instanse=new Singleton();

    public static Singleton getInstanse() {
        return instanse;
    }
    private Singleton(){ }
}

class SingletonLazy{
    private static volatile SingletonLazy instance=null;
    private static Object locker=new Object();
    public static SingletonLazy getInstance() {
        if (instance==null){
            synchronized (locker){
                if (instance==null){
                    instance=new SingletonLazy();
                }
            }
        }

        return instance;
    }
    private SingletonLazy(){ }
}

public class Test {
}

*/































/*
import java.util.PriorityQueue;

class MyTimertask implements Comparable<MyTimertask>{
    private long time;
    private Runnable runnable;

    public MyTimertask(Runnable runnable,long delay){
        this.runnable=runnable;
        this.time=System.currentTimeMillis()+delay;
    }

    public long getTime() {
        return time;
    }

    public void run(){
        runnable.run();
    }

    @Override
    public int compareTo(MyTimertask o) {
        return (int) (this.time-o.time);
    }
}




class MyTimer{
    private Thread t;
    private PriorityQueue<MyTimertask> queue=new PriorityQueue();
    private Object locker=new Object();
    public void schedule(Runnable runnable,long delay){
        synchronized (locker){
            MyTimertask task=new MyTimertask(runnable,delay);
            queue.offer(task);
            locker.notify();
        }
    }


    private MyTimer(){
        t=new Thread(()->{
         try {
             while (true){
                 while (queue.isEmpty()){
                     locker.wait();
                 }
                 MyTimertask curTask= queue.peek();
                 if (System.currentTimeMillis()>=curTask.getTime()){
                     curTask.run();
                     queue.poll();
                 }else {
                     locker.wait(curTask.getTime()-System.currentTimeMillis());
                 }
             }
         }catch (InterruptedException e){
             e.printStackTrace();
         }
        });
        t.start();
    }
}

*/




































/*
class MyBlockingQueue{
    private String[] elems;
    public MyBlockingQueue(int capcity){
        elems=new String[capcity];
    }

    private int head;
    private int tail;
    private int size;
    private Object lockker=new Object();

    public void put(String str) throws InterruptedException {
       synchronized (lockker){
           while (size==elems.length){
               lockker.wait();
           }
           elems[tail]=str;
           tail++;
           if (tail==elems.length){
               tail=0;
           }
           size++;
           lockker.notify();
       }

    }

    public String take() throws InterruptedException {
        String elem=null;
        synchronized (lockker){
            while (size==0){
                lockker.wait();
            }
            elem=elems[head];
            head++;
            if (head==elems.length){
                head=0;
            }
            size--;
            lockker.notify();
        }
        return elem;
    }
}






public class Test {
    public static void main(String[] args) throws InterruptedException {
        MyBlockingQueue queue=new MyBlockingQueue(10);
        Thread t1=new Thread(()->{
            int i=0;
           while (true){
               System.out.println("生产元素"+i);
               i++;
               try {
                   queue.put(i+" ");
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }

        });
        Thread t2=new Thread(()->{
           int i=0;
           while (true){
               System.out.println("消费元素"+i);
               try {
                   i++;
                   queue.take();
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
}
*/
